import { createStore } from 'vuex';

export default createStore({
    state: {
        cartList: [], //购物车列表数据
        orderList: [], //生成订单列表数据
        orderListed:[],//生成订单后
        edit: true, //编辑的状态
        userAddress: [
            {
                id: 1001,
                name: '小卜',
                tel: '13123456731',
                province: '浙江省',
                city: '杭州市',
                county: '余杭区',
                addressDetail: '杭州师范大学',
                isDefault: true,
                areaCode: '110101',
            },
            {
                id: 1001,
                name: '小卜',
                tel: '13123456731',
                province: '浙江省',
                city: '杭州市',
                county: '余杭区',
                addressDetail: '杭州师范大学',
                isDefault: false,
                areaCode: '110101',
            },
        ],
    },
    mutations: {
        //购物车
        ADDCART(state, value) {
            state.cartList = value;
        },
        //结算按钮
        PAY(state, value) {
            state.orderList = value;
        },
        //删除
        DELETE(state, value) {
            state.cartList = value;
        },
        //编辑按钮
        EDIT(state, value) {
            if (value === 'delete') {
                state.edit = true;
            } else {
                state.edit = !state.edit;
            }
        },
        //生成订单
        UPDATEORDER(state){
            state.orderListed=state.orderListed.concat(state.orderList);
        },
        //新增地址
        ADDADDRESS(state, value) {
            state.userAddress.map((item) => {
                if (value.isDefault) {
                    item.isDefault = false;
                }
            });
            state.userAddress.push(value);
        },
        //编辑地址
        CHANGEADDRESS(state, value) {
            state.userAddress = state.userAddress.map((item) => {
                if (value.isDefault) {
                    item.isDefault = false;
                }
                return item.id === value.id ? value : item;
            });
        },
        //地址删除按钮
        DELETEADDRESS(state, value) {
            state.userAddress = state.userAddress.filter((item) => {
                return !(value.id === item.id);
            });
            if (value.isDefault && state.userAddress.length) {
                state.userAddress[0].isDefault = true;
            }
        },
    },
    actions: {},
});
